<?php
/**
 * Name: 轮播模型.
 * User: 董坤鸿
 * Date: 2017/11/22
 * Time: 10:07
 */

namespace app\model;

use think\Model;

class Banners extends Model
{
    protected $pk = 'id';
    protected $table = 'g_banners';
    protected $insert = ['created_at'];
    protected $update = ['updated_at'];

    /**
     * 创建时间
     *
     * @param $value
     * @return false|string
     */
    protected function setCreatedAtAttr($value)
    {
        return date('Y-m-d H:i:s');
    }

    /**
     * 修改时间
     *
     * @param $value
     * @return false|string
     */
    protected function setUpdatedAtAttr($value)
    {
        return date('Y-m-d H:i:s');
    }

    /**
     * 获取模板列表
     *
     * @param  array $data
     * @param int $limit
     * @return \think\Paginator
     */
    public function lists($data, $limit = 20)
    {
        $map = [];
        if(!empty($data)){
            if (isset($data['keywords'])){
                $keywords = $data['keywords'];
                $map['title']  = ['like',"%$keywords%"];
            }
            if (isset($data['status']) && $data['status'] !== ''){
                $map['status'] = $data['status'];
            }
        }
        $result = $this->where($map)->order('sort', 'ASC')->paginate($limit);
        return $result;
    }

    /**
     * 插入轮播
     *
     * @param $data
     * @return array
     */
    public function store($data)
    {
        //验证数据并执行添加
        $result = $this->validate(true)->save($data);
        if (false === $result) {
            // 验证失败 输出错误信息
            return ['valid' => 0, 'msg' => $this->getError()];
        } else {
            return ['valid' => 1, 'msg' => '添加成功'];
        }
    }

    /**
     * 查询轮播
     *
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     */
    public function show($id)
    {
        return $this->find($id);
    }

    /**
     * 修改轮播
     *
     * @param $data
     * @return array
     */
    public function edit($data)
    {
        //验证数据并执行添加
        $result = $this->validate(true)->allowField( true )->save($data, [$this->pk=>$data['id']]);
        if (false === $result) {
            // 验证失败 输出错误信息
            return ['valid' => 0, 'msg' => $this->getError()];
        } else {
            return ['valid' => 1, 'msg' => '编辑成功'];
        }
    }

    /**
     * 删除轮播
     *
     * @param $data
     * @return array
     */
    public function del($data)
    {
        if (self::destroy($data['id'])) {
            return ['valid' => 1, 'msg' => '删除成功'];
        } else {
            return ['valid' => 0, 'msg' => '删除失败'];
        }
    }

    /**
     * 轮播排序
     *
     * @param $data
     * @return array
     */
    public function sort($data)
    {
        $result = $this->validate(
            [
                'sort' => 'require|between:1,9999',
            ], [
                'sort.require' => '请输入排序',
                'sort.between' => '排序需要在1-9999之间',
            ]
        )->save($data, [$this->pk => $data['id']]);
        if ($result) {
            return ['valid' => 1, 'msg' => '操作成功'];
        } else {
            return ['valid' => 0, 'msg' => $this->getError()];
        }
    }
}